package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.EmsEnergySchedule;
import com.econ.powercloud.ems.dal.mapper.EmsEnergyScheduleMapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Repository;

/**
 * @author zjr
 * @create 2021/7/22 下午2:31
 * @description
 */
@Repository
public class EmsEnergyScheduleDAO extends ServiceImpl<EmsEnergyScheduleMapper, EmsEnergySchedule> {

    public void addEnergySchedule(EmsEnergySchedule emsEnergySchedule) {
        this.baseMapper.insert(emsEnergySchedule);
    }

    public void demandControlEnabled(EmsEnergySchedule emsEnergySchedule, Integer enabled) {
        LambdaUpdateWrapper<EmsEnergySchedule> emsEnergyScheduleLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        emsEnergyScheduleLambdaUpdateWrapper.eq(EmsEnergySchedule::getEnergyStationId,emsEnergySchedule.getEnergyStationId());
        emsEnergyScheduleLambdaUpdateWrapper.eq(EmsEnergySchedule::getEnergyEquipmentId,emsEnergySchedule.getEnergyEquipmentId());
        emsEnergyScheduleLambdaUpdateWrapper.set(EmsEnergySchedule::getDemandControlEnabled, enabled);
        this.baseMapper.update(emsEnergySchedule, emsEnergyScheduleLambdaUpdateWrapper);
    }

    public void updateSocControlEnable(EmsEnergySchedule schedule,Integer enabled){
        LambdaUpdateWrapper<EmsEnergySchedule> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(EmsEnergySchedule::getEnergyStationId,schedule.getEnergyStationId());
        wrapper.eq(EmsEnergySchedule::getEnergyEquipmentId,schedule.getEnergyEquipmentId());
        wrapper.set(EmsEnergySchedule::getSocControlEnabled, enabled);
        this.baseMapper.update(schedule,wrapper);
    }

    public void backupPowerEnabled(EmsEnergySchedule emsEnergySchedule, Integer enabled) {
        LambdaUpdateWrapper<EmsEnergySchedule> emsEnergyScheduleLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        emsEnergyScheduleLambdaUpdateWrapper.eq(EmsEnergySchedule::getEnergyStationId,emsEnergySchedule.getEnergyStationId());
        emsEnergyScheduleLambdaUpdateWrapper.eq(EmsEnergySchedule::getEnergyEquipmentId,emsEnergySchedule.getEnergyEquipmentId());
        emsEnergyScheduleLambdaUpdateWrapper.set(EmsEnergySchedule::getBackupPowerEnabled, enabled);
        this.baseMapper.update(emsEnergySchedule, emsEnergyScheduleLambdaUpdateWrapper);
    }

    public void inversePowerOutputEnabled(EmsEnergySchedule emsEnergySchedule, Integer enabled) {
        LambdaUpdateWrapper<EmsEnergySchedule> emsEnergyScheduleLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        emsEnergyScheduleLambdaUpdateWrapper.eq(EmsEnergySchedule::getEnergyStationId,emsEnergySchedule.getEnergyStationId());
        emsEnergyScheduleLambdaUpdateWrapper.eq(EmsEnergySchedule::getEnergyEquipmentId,emsEnergySchedule.getEnergyEquipmentId());
        emsEnergyScheduleLambdaUpdateWrapper.set(EmsEnergySchedule::getInversePowerOutputEnabled, enabled);
        this.baseMapper.update(emsEnergySchedule, emsEnergyScheduleLambdaUpdateWrapper);
    }

    public EmsEnergySchedule getByStationIdByEquipmentId(String energyStationId, String energyEquipmentId) {
        LambdaQueryWrapper<EmsEnergySchedule> emsEnergyScheduleLambdaQueryWrapper = Wrappers.lambdaQuery();
        emsEnergyScheduleLambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(energyStationId), EmsEnergySchedule::getEnergyStationId, energyStationId);
        emsEnergyScheduleLambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(energyEquipmentId), EmsEnergySchedule::getEnergyEquipmentId, energyEquipmentId);
        return this.baseMapper.selectOne(emsEnergyScheduleLambdaQueryWrapper);
    }
}
